package com.xjm.ips.service;

import com.xjm.ips.entity.*;
import com.xjm.ips.mapper.EmployeeMapper;
import com.xjm.ips.mapper.EmployeeremoveMapper;
import com.xjm.ips.utils.Hrutils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
public class EmployeeBasiService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    EmployeeremoveMapper employeeremoveMapper;
    @Autowired
    ViewService viewService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    public static final Logger logger = LoggerFactory.getLogger(EmployeeBasiService.class);

    public Integer getjobNo() {
        return employeeMapper.getjobNo();
    }

    @Transactional
    public Integer deleteEmployeeById(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);//离职员工信息
        employee.setId(null);
        employee.setLeaveWorkDate(new Date());
        employee.setWorkState("离职");
        employeeMapper.insertSelectiveToNotWork(employee);
        Integer result =  employeeMapper.deleteByPrimaryKey(id);
        viewService.initDeparmentNumbersOfRedis();
        return  result;
    }

    public Integer addEmployees(List<Employee> list) {
        Integer integer = employeeMapper.addEmployees(list);
        viewService.initDeparmentNumbersOfRedis();
        return integer;
    }

    @Transactional
    public MyPage getEmpOfNotWork(Integer pageNo, Integer pageSize,Employee employee) {
        if(pageNo!=null && pageSize!=null){
            pageNo = (pageNo-1)*pageSize;
        }
        List<Employee> data = employeeMapper.getEmpOfNotWork(pageNo,pageSize,employee);
        Long total = employeeMapper.getTotalOfNotWork(employee);
        MyPage myPage = new MyPage();
        myPage.setData(data);
        myPage.setTotal(total);
        return myPage;
    }

    /**
     * 新增数据
     * @param employee
     * @return
     */
    @Transactional
    public Integer addEmployee(Employee employee) {
        calcContractYear(employee);
        int i = employeeMapper.insertSelective(employee);
        if(i==1){
            Employee employee1 = employeeMapper.selectByPrimaryKey(employee.getId());
            logger.info(employee1.toString());
            rabbitTemplate.convertAndSend("jaymin.mail.test",employee1);
        }
        viewService.initDeparmentNumbersOfRedis();
        return i;
    }

    @Transactional
    public MyPage getEmployeeByPageWithSalary(Integer pageNo, Integer pageSize) {
        if(pageNo!=null && pageSize!=null){
            pageNo = (pageNo-1)*pageSize;
        }

        List<Employee> data = employeeMapper.getEmployeeByPageWithSalary(pageNo,pageSize);
        Long total = employeeMapper.getTotal(null,null);
        MyPage myPage = new MyPage();
        myPage.setData(data);
        myPage.setTotal(total);
        return myPage;
    }

    public Integer updateEmploeeOfSalaryById(Integer employeeId, Integer salaryId) {
        return employeeMapper.updateEmploeeOfSalaryById(employeeId,salaryId);
    }

    public Employee getEmpByJobNo(String jobNo) {
        return employeeMapper.getEmpByWorkID(jobNo);
    }

    @Transactional
    public Integer updateEmployee(Employee employee) {
        Employee old = employeeMapper.selectByPrimaryKey(employee.getId());
        if (!old.getDepartmentId().equals(employee.getDepartmentId())) {
            Employeeremove employeeremove = new Employeeremove();
            employeeremove.setEmpName(employee.getName());
            employeeremove.setEmpWorkID(employee.getJobNo());
            employeeremove.setRemoveDate(new Date());
            employeeremove.getAfterDep().setId(employee.getId());
            employeeremove.getBeforeDep().setId(old.getDepartmentId());
            employeeremoveMapper.insertSelective(employeeremove);
        }
        calcContractYear(employee);
        int i = employeeMapper.updateByPrimaryKeySelective(employee);
        viewService.initDeparmentNumbersOfRedis();
        return i;
    }

    public List<Employee> getInnerEmpWithSalary(String workid){
        return employeeMapper.selectEmployeeWithSalary(workid);
    }

    /**
     * 新增和修改数据时自动计算合同期限
     * @param employee
     */
    public void calcContractYear(Employee employee){
        SimpleDateFormat year = new SimpleDateFormat("yyyy");//年
        SimpleDateFormat month = new SimpleDateFormat("MM");//月
        DecimalFormat decimalFormat = new DecimalFormat("##.00");//差值
        Date beginContract = employee.getBeginContract();//开启日期
        Date endContract = employee.getEndContract();//终止日期
        //(终止日期的年份-开启日期的年份)*12-(终止日期月份-开启日期的月份)
        double result = (Double.parseDouble(year.format(endContract)) - Double.parseDouble(year.format(beginContract))) * 12 +
                ((Double.parseDouble(month.format(endContract))) - Double.parseDouble(month.format(beginContract)));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(result/12)));
    }


    @Transactional
    public MyPage getEmployee(Integer pageNo, Integer pageSize,Employee employee) {
        String depPath = Hrutils.getMydepPath();//当前登陆用户的部门索引
        if(pageNo!=null && pageSize!=null){
            pageNo = (pageNo-1)*pageSize;
        }
        List<Employee> data = employeeMapper.getEmployee(pageNo,pageSize,employee,depPath);
        Long total = employeeMapper.getTotal(employee,depPath);
        MyPage myPage = new MyPage();
        myPage.setData(data);
        myPage.setTotal(total);
        return myPage;
    }

    public Integer deleteEmpRemove(Integer id) {
        return employeeMapper.deleteByPrimaryKeyOfRemove(id);
    }

    @Transactional
    public Integer batchDeleteEmployeeByIds(Integer[] ids) {
        for (Integer id : ids) {
            Employee employee = employeeMapper.selectByPrimaryKey(id);//离职员工信息
            employee.setId(null);
            employee.setLeaveWorkDate(new Date());
            employee.setWorkState("离职");
            employeeMapper.insertSelectiveToNotWork(employee);
        }
        Integer integer = employeeMapper.batchDeleteEmployeeByIds(ids);
        viewService.initDeparmentNumbersOfRedis();
        return integer;
    }
}
